package com.jrsoft.bim.db;

import com.jrsoft.bim.util.DateUtil;
import com.jrsoft.bim.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.DbUtils;

import java.sql.*;
import java.util.*;

/**
 * ${DESCRIPTION}
 *
 * @author Blueeyedboy
 * @create 2019-12-20 4:40 PM
 **/
@Slf4j
public class ConnDB {
	public static final String DRIVER_CLASS = "org.sqlite.JDBC";
	private ResultSet rs = null;
	private Connection ct = null;
	private Statement sm = null;
	PreparedStatement ps = null;
	private Long rowCount = Long.valueOf(0L);
	private int pageCount = 0;
	private String driver;
	private String url;
	private String user;
	private String pwd;
	StringBuilder sqlBuilder = new StringBuilder();

	public String getSql(){
		return sqlBuilder.toString();
	}

	public ConnDB(String driver, String url, String user, String pwd) {
		this.driver = driver;
		this.url = url;
		this.user = user;
		this.pwd = pwd;
	}

	public ConnDB(String url, String user, String pwd) {

		this.driver = DRIVER_CLASS;
		this.url = url;
		this.user = user;
		this.pwd = pwd;
	}


	public Connection getConnection() {
		Connection connection;
		try {
			Class.forName(DRIVER_CLASS);
			connection = DriverManager.getConnection(url, this.user, this.pwd);
		} catch (Exception e) {
			log.error("数据库连接失败", e);
			throw new RuntimeException(e);
		}
		return connection;
	}

	public void releaseConnection(final Connection connection) {
		if (connection != null) {
			try {
				DbUtils.close(connection);
			} catch (SQLException e) {
				try {
					connection.close();
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
				log.error("测试数据库连接后释放资源失败", e);
			}
		}
	}


	public void executeUpdate() {
		executeUpdate(sqlBuilder.toString());
		FileUtil.writeToFile("E:\\工作\\项目\\bim\\开发\\2019-1-2\\汇总\\result.sql",sqlBuilder.toString(),true);
	}

	public void beginTransaction(){
		try {
			if(ct == null){
				this.ct = this.getConnection();

			}
			ct.setAutoCommit(false);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void commitTransaction(){
		try {
			ct.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public int executeUpdateNotClose(String sql) {
		int i = 0;
		try {
			ps = ct.prepareStatement(sql);
			int var5 = ps.executeUpdate();
			return var5;
		} catch (Exception var15) {
			var15.printStackTrace();
		}

		return i;
	}
	public int executeUpdate(String sql) {

		int i = 0;
		Connection conn = null;
		try {
			conn = this.getConnection();
			ps = conn.prepareStatement(sql);
			int var5 = ps.executeUpdate();
			return var5;
		} catch (Exception var15) {
			var15.printStackTrace();
		} finally {
			try {
				if(ps != null) {
					ps.close();
				}

				if(conn != null) {
					conn.close();
				}
			} catch (SQLException var14) {
				var14.printStackTrace();
			}

		}

		return i;
	}
	public int executeUpdateSql(String sql) {
		if(true){
//			System.out.println(sql);
			sqlBuilder.append(sql);
			return 0;
		}
		return 0;
	}

	public Vector<Map<String, Object>> getData(String sql) {
		Vector al = new Vector();

		try {
			this.ct = this.getConnection();
			this.sm = this.ct.createStatement();
			this.rs = this.sm.executeQuery(sql);

			while(this.rs.next()) {
				Map<String, Object> m = new HashMap();

				for(int i = 1; i <= this.rs.getMetaData().getColumnCount(); ++i) {
					if("datetime".equals(this.rs.getMetaData().getColumnTypeName(i))) {
						m.put(this.rs.getMetaData().getColumnLabel(i), DateUtil.getDateTimeStr(new java.util.Date(this.rs.getTimestamp(i).getTime())));
					} else if("float".equals(this.rs.getMetaData().getColumnTypeName(i).toLowerCase())) {
						m.put(this.rs.getMetaData().getColumnLabel(i), this.rs.getObject(i));
					} else {
						m.put(this.rs.getMetaData().getColumnLabel(i), this.rs.getString(i));
					}
				}

				al.add(m);
			}
		} catch (Exception var8) {
			var8.printStackTrace();
		} finally {
			this.close();
		}

		return al;
	}

	public Map<String, Object> getSingleData(String sql) {
		HashMap m = null;
		Vector<Map<String, Object>> data = getData(sql);
		if(data !=null && data.size()>0){
			return data.get(0);
		}
		return m;
	}


	public Long getCount(String sql) {
		try {
			this.ct = this.getConnection();
			this.sm = this.ct.createStatement();
			this.rs = this.sm.executeQuery(sql);
			if(this.rs.next()) {
				this.rowCount = Long.valueOf(this.rs.getLong(1));
			}
		} catch (Exception var6) {
			var6.printStackTrace();
		} finally {
			this.close();
		}

		return this.rowCount;
	}
	public void close() {
		try {
			if(this.ps != null) {
				this.ps.close();
				this.ps = null;
			}

			if(this.rs != null) {
				this.rs.close();
				this.rs = null;
			}

			if(this.sm != null) {
				this.sm.close();
				this.sm = null;
			}

			if(this.ct != null) {
				this.ct.close();
				this.ct = null;
			}
		} catch (Exception var2) {
			var2.printStackTrace();
		}

	}
}
